﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Xml.Linq;
using Grasshopper.GUI;
using Grasshopper.GUI.Base;
using Grasshopper.Kernel;
using Rhino.Geometry;
using Rhino.Render;

namespace D009_AdditionMenu
{
    public class Component_CreateHuman : GH_Component
    {
        protected override System.Drawing.Bitmap Icon => Properties.Resources.human;
        public override Guid ComponentGuid => new Guid("BC798066-8D6A-493B-8AEB-8326628B6E79");
        public Gender gender = Gender.男;
        public State state = State.工作;
        public bool married = false;

        public Component_CreateHuman()
          : base("Component_CreateHuman", "Nickname",
              "Description",
              "MyPlugin", "AdditionMenu")
        {
        }
        protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddIntegerParameter("ID", "id", "这个人的编号ID", GH_ParamAccess.item, 0);
            pManager.AddTextParameter("姓名", "n", "这个人的名字", GH_ParamAccess.item, "");
            pManager.AddIntegerParameter("年龄", "age", "这个人的年龄", GH_ParamAccess.item, 0);
        }

        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
            pManager.AddParameter(new Param_Human());
        }
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            int id = 0;
            string name = "";
            int age = 0;

            if (!DA.GetData(0, ref id)) return;
            if (!DA.GetData(1, ref name)) return;
            if (!DA.GetData(2, ref age)) return;

            Human human = new Human()
            {
                Id = id,
                Name = name,
                Age = age,
                Gender = gender,
                State = state,
                Married = married
            };

            DA.SetData(0, new GH_Human(human));
        }


        public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
        {
            base.AppendAdditionalMenuItems(menu);

            Menu_AddGenderMenu(menu);
            Menu_AddMarriedMenu(menu);
            Menu_AddStateMenu(menu);
            //GH_DocumentObject.Menu_AppendColourPicker(menu, Color.Red, aaa);
            Menu_AddDigitScroller(menu);
            GH_DocumentObject.Menu_AppendSeparator(menu);
        }

        private void Menu_AddDigitScroller(ToolStripDropDown menu)
        {
            //var dc = GH_DocumentObject.Menu_AppendDigitScrollerItem(menu, 0, 100, 20, 3);
            var dc = new GH_DigitScroller
            {
                MinimumValue = 0,
                MaximumValue = 100,
                DecimalPlaces = 3,
                Value = 25
            };
            dc.Prefix = "前缀文本";
            dc.Height = menu.Items[0].Height; // 23
            dc.ValueChanged += ChangeValue;
            GH_DocumentObject.Menu_AppendCustomItem(menu, dc);

        }

        private void ChangeValue(object sender, GH_DigitScrollerEventArgs e)
        {
            decimal v = e.Value;
        }

        private void aaa(GH_ColourPicker sender, GH_ColourPickerEventArgs e)
        {
            MessageBox.Show("Test");
        }

        private void Menu_AddStateMenu(ToolStripDropDown menu)
        {
            ToolStripComboBox cb = new ToolStripComboBox();
            //ComboBox cb = new ComboBox();
            cb.FlatStyle = FlatStyle.Standard;
            cb.DropDownStyle = ComboBoxStyle.DropDownList;

            foreach (State s in Enum.GetValues(typeof(State)))
            {
                cb.Items.Add(s);
            }
            cb.SelectedItem = state;
            cb.SelectedIndexChanged += ChangeState;

            menu.Items.Add(cb);
            //GH_DocumentObject.Menu_AppendCustomItem(menu, cb);
        }

        private void ChangeState(object sender, EventArgs e)
        {
            var cb = (ToolStripComboBox)sender;
            if ((State)cb.SelectedItem != state)
            {
                state = (State)cb.SelectedItem;
                ExpireSolution(true);
            }
        }

        private void Menu_AddMarriedMenu(ToolStripDropDown menu)
        {
            GH_DocumentObject.Menu_AppendItem(menu, "已婚", ChangeMarried, Image.FromFile($"Icons/婚姻.png"), true, married); ;
        }

        private void ChangeMarried(object sender, EventArgs e)
        {
            married = !married;
            ExpireSolution(true);
        }

        public void Menu_AddGenderMenu(ToolStripDropDown menu)
        {
            ToolStripMenuItem genderMenus = GH_DocumentObject.Menu_AppendItem(menu, "性别", null);

            foreach (Gender g in Enum.GetValues(typeof(Gender)))
            {
                string name = g.ToString();
                Image icon = Image.FromFile($"Icons/{name}.png");
                GH_DocumentObject.Menu_AppendItem(genderMenus.DropDown, name, ChangeGender, icon, true, g == gender);
            }

            GH_DocumentObject.Menu_MoveItem(genderMenus, false, "Set one 人");
        }

        private void ChangeGender(object sender, EventArgs e)
        {
            if (gender == Gender.男)
            {
                gender = Gender.女;
            }
            else
            {
                gender = Gender.男;
            }
            ExpireSolution(true);
        }
    }
}